package com.zjtobacco.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjtobacco.dao.BaseDao;
import com.zjtobacco.dao.RegionDao;
import com.zjtobacco.domain.Region;
import com.zjtobacco.domain.Warehouse;
import com.zjtobacco.service.RegionService;
import com.zjtobacco.service.WarehouseService;
@Service
public class RegionServiceImpl extends BaseServiceImpl<Region, Integer>
		implements RegionService {

	@Autowired
	private RegionDao regionDao;
	@Autowired
	private WarehouseService warehouseService;

	@Override
	public BaseDao<Region, Integer> getDao() {
		return regionDao;
	}
	@Override
	public void fillDetail(Region region) throws Exception {
		Warehouse warehouse = this.warehouseService.get(region.getWarehouseId());
		region.setWarehouseName(warehouse.getName());
	
		Region regionCapacity = getCapacityById(region.getId());
		if(regionCapacity==null){
			region.setLevelCommonCapacity(0);
			region.setLevelCoreCapacity(0);
			region.setLevelRelatedCapacity(0);
			return;
		}
		region.setLevelCoreCapacity(regionCapacity.getLevelCoreCapacity()!=null?regionCapacity.getLevelCoreCapacity():0);
		region.setLevelRelatedCapacity(regionCapacity.getLevelRelatedCapacity()!=null?regionCapacity.getLevelRelatedCapacity():0);
		region.setLevelCommonCapacity(regionCapacity.getLevelCommonCapacity()!=null?regionCapacity.getLevelCommonCapacity():0);
	}

	@Override
	public Region getCapacityById(Integer id) throws Exception{
		return this.regionDao.getCapacityById(id);
	}
}
